
Page({
  data: {
    messageList: [], // 用于存储消息列表的数据
    inputValue: "" // 用于存储用户输入的数据
  },
  nullEmpty: function () {
   
  },
  sendMessage: function(e) {
    if (this.data.inputValue.match(/^[ ]*$/)) {
      wx.showModal({
        title: '不能发送空白信息',
        showCancel: false,
      });
    } else {
      wx.showLoading({
        title: '加载中',
      });
      var that =this
      this.data.messageList.push({
        tx: this.data.inputValue, // 用户发送的消息
        rx: 'loading...', // 回复状态
      });
      this.createSelectorQuery().select('.scroll-wrap').node(function (res) {
        let ScrollViewContext = res.node;
        ScrollViewContext.scrollIntoView(`#tx-item-${that.data.messageList.length - 1}`);
      }).exec();
       // ... 构造请求参数
    const requestParams = {
      // ... 构造请求参数
      model: "abab5.5-chat",
      tokens_to_generate: 1024,
      temperature: 0.9,
      top_p: 0.95,
      stream: false,
      reply_constraints: {
        sender_type: "BOT",
        sender_name: "MM智能助理",
      },
      messages: [
        {
          sender_type: "USER",
          sender_name: "用户",
          text: this.data.inputValue,
        },
      ],
    };
    console.log('页面加载了');
    console.log();
    wx.serviceMarket.invokeService({
      service: 'wx1ef79fe5f143a445', // 替换为您的服务ID
      api: 'ChatCompletionPro', // 替换为您的API名称
      data: requestParams
    }).then(res => {
      // 处理返回数据
      let messageList = this.data.messageList;
      messageList[messageList.length - 1].rx = res.data.reply; // 更新回复
      this.setData({
        messageList: messageList,
        inputValue:""
      });
      this.createSelectorQuery().select('.scroll-wrap').node(function (res) {
        let ScrollViewContext = res.node;
        ScrollViewContext.scrollIntoView(`#rx-item-${that.data.messageList.length - 1}`);
      }).exec();
      wx.hideLoading();
    }).catch(error => {
      // 处理错误
      console.error(error);
      wx.showModal({
        title: '请求失败',
        content: '请检查网络连接或服务状态',
      });
    });
    }
  }
});